<?php

namespace plugin\ycMassage\utils;

use app\common\manager\SettingsMgr;
use GuzzleHttp\Client;
use think\facade\Cache;
use think\facade\Log;

class WechatHelper
{
    public static function getAccessToken($saas_appid)
    {
        $config = getHpConfig('', $saas_appid, 'wx');
        if (empty($config['state'])) {
            throw new \Exception('请先配置小程序信息');
        }
        $redis_key = 'yc_massage_access_token.' . $saas_appid.'.'.$config['appid'];
        $access_token = Cache::get($redis_key);
        if ($access_token !== null) {
            return $access_token;
        }
        $query = [
            'appid' => $config['appid'],
            'secret' => $config['secret'],
            'grant_type' => 'client_credential'
        ];
        $url = 'https://api.weixin.qq.com/cgi-bin/token?' . http_build_query($query);
        $client = new Client();
        $response = $client->get($url);
        $data = json_decode($response->getBody()->getContents(), true);
        if (isset($data['errcode'])) {
            throw new \Exception("[{$data['errcode']}][{$query['appid']}]" . $data['errmsg']);
        }
        Cache::set($redis_key, $data['access_token'], $data['expires_in'] - 60);
        return $data['access_token'];
    }
    public static function getMPAccessToken($saas_appid)
    {
        $applet = SettingsMgr::group($saas_appid, 'applet');
        if (empty($applet['applet_appid']) || empty($applet['applet_secret'])) {
            throw new \Exception('请先配置小程序信息');
        }
        $redis_key = 'yc_massage_mp_access_token.' . $saas_appid.'.'.$applet['applet_appid'];
        $access_token = Cache::get($redis_key);
        if ($access_token !== null) {
            return $access_token;
        }
        $query = [
            'appid' => $applet['applet_appid'],
            'secret' => $applet['applet_secret'],
            'grant_type' => 'client_credential'
        ];
        $url = 'https://api.weixin.qq.com/cgi-bin/token?' . http_build_query($query);
        $client = new Client();
        $response = $client->get($url);
        $data = json_decode($response->getBody()->getContents(), true);
        if (isset($data['errcode'])) {
            throw new \Exception("[{$data['errcode']}]" . $data['errmsg']);
        }
        Cache::set($redis_key, $data['access_token'], $data['expires_in'] - 60);
        return $data['access_token'];
    }
    public static function templateSend($saas_appid, $params)
    {
        $access_token = WechatHelper::getAccessToken($saas_appid);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token;
        $client = new Client([
            'content_type' => 'application/json',
        ]);
        $response = $client->post($url, ['body' => json_encode($params, JSON_UNESCAPED_UNICODE)]);
        $data = json_decode($response->getBody()->getContents(), true);
        if (empty($data['msgid'])) {
            throw new \Exception("[{$data['errcode']}]" . $data['errmsg']);
        }
        return $data['msgid'];
    }
}
